var express = require('express');
var pool = require('../pool');
// const app = require('../app');
var router = express.Router();


/* GET users listing. */
// router.get('/', function(req, res, next) {
//   res.send('respond with a resource');
// });

// 用户注册
router.post('/reg', (req, res) => {
  let uname = req.body.username;
  let upwd = req.body.password;
  let sql = "SELECT COUNT(uid) AS count FROM menu_user WHERE uname=?";
  pool.query(sql, [uname], (error, results) => {
    if (error) throw error;
    let count = results[0].count;
    if (count == 0) {
      let sql = 'insert into menu_user (uid,uname,upwd) values(null,?,?)';
      pool.query(sql, [uname, upwd], (error, result) => {
        if (error) throw error;
        res.send({ code: 200, msg: '注册成功！' })
      })
    } else {
      res.send({ code: 201, msg: "用户名已存在" });
    }
  })
})

// 用户登入
router.post('/login', (req, res) => {
  var obj = req.body
  var sql = "SELECT * FROM menu_user WHERE uname=? AND upwd=?";
  pool.query(sql, [obj.uname, obj.upwd], (error, results) => {
    if (error) throw error;
    if (results.length == 0) { //登录失败
      res.send({ message: 'login failed', code: 201 });
    } else {                 //登录成功
      res.send({ message: 'ok', code: 200, result: results[0] });
    }
  });
});

// 用户信息
router.get('/userinfo', (req, res) => {
  var obj = req.query
  var sql = "SELECT * FROM menu_user WHERE uname=?";
  pool.query(sql, [obj.uname], (error, results) => {
    if (error) throw error;
    if (results.length == 0) { //登录失败
      res.send({ message: 'login failed', code: 201 });
    } else {                 //登录成功
      res.send({ message: 'ok', code: 200, result: results[0] });
    }
  });
})

// 查询收藏
router.get('/collect', (req, res) => {
  var obj = req.query;
  var sql = "SELECT * FROM menu_collect WHERE user_id=?";
  pool.query(sql, [obj.uid], (error, results) => {
    if (error) throw error;
    if (results[0]) {
      res.send({ message: 'ok', code: 200, result: results });
    } else {
      res.send({
        msg: "未查询到",
        code: 201
      })
    }
  });
})

// 添加收藏
router.post('/addcollect', (req, res) => {
  let uid = req.body.uid;
  let cid = req.body.cid;
  let sql = "SELECT * FROM menu_collect WHERE user_id=? AND collect=?";
  pool.query(sql, [uid, cid], (error, results) => {
    if (error) throw error;
    console.log(results)
    if (results.length == 0) {
      let sql = 'insert into menu_collect (coid,user_id,collect) values(null,?,?)';
      pool.query(sql, [uid, cid], (error, result) => {
        if (error) throw error;
        res.send({ code: 200, msg: '添加成功' })
      })
      console.log("aaa")
    } else {
      res.send({ code: 201, msg: "已经收藏了" });
    }
  })
})
// 取消收藏
router.post('/uncollect', (req, res) => {
  let uid = req.body.uid;
  let cid = req.body.cid;
  let sql = "DELETE FROM menu_collect WHERE user_id =?and collect=?";
  pool.query(sql, [uid, cid], (error, results) => {
    if (error) throw error;
    console.log(results)
    if (results.affectedRows == 1) {
      res.send({ code: 200, msg: '取消成功' })
    } else {
      res.send({ code: 201, msg: "取消收藏失败" });
    }
  })
})

// 查询收藏
router.post('/iscollect', (req, res) => {
  let uid = req.body.uid;
  let cid = req.body.cid;
  let sql = "SELECT * FROM menu_collect WHERE user_id=? AND collect=?";
  pool.query(sql, [uid, cid], (error, results) => {
    if (error) throw error;
    console.log(results)
    if (results.length == 0) {
      res.send({ code: 200, msg: '暂时没有收藏' })
    } else {
      res.send({ code: 201, msg: "已经收藏了" });
    }
  })
})

// 修改昵称nickname
router.post('/upnickname', (req, res) => {
  let uid = req.body.uid;
  let nickname = req.body.nickname
  let sql = "UPDATE menu_user SET nickname = ? WHERE menu_user.uid = ?;"
  pool.query(sql,[nickname,uid],(error,result)=>{
    if(error) throw error
    console.log(result)
    if (result.affectedRows == 1) {
      res.send({ code: 200, msg: "修改成功" })
    } else {
      res.send({ code: 201, msg: "修改失败" });
    }
  })
})

// 修改个性签名
router.post('/upsign', (req, res) => {
  let uid = req.body.uid;
  let sign = req.body.sign
  let sql = "UPDATE menu_user SET sign = ? WHERE menu_user.uid = ?;"
  pool.query(sql,[sign,uid],(error,result)=>{
    if(error) throw error
    console.log(result)
    if (result.affectedRows == 1) {
      res.send({ code: 200, msg: "修改成功" })
    } else {
      res.send({ code: 201, msg: "修改失败" });
    }
  })
})
  // 验证用户名是否存在
  // router.get("/v1",(req,res)=>{
  //   var obj=req.query;
  //   var sql="select uid from menu_user where uname=?";
  //   console.log(sql,obj);
  //   pool.query(sql,[obj.uname],(err,result)=>{
  //       if(err) throw err;
  //       if(result.length>0){
  //           res.send({
  //             code:200,
  //             msg:"用户名存在"
  //           })
//       }else{
//           res.send("0");
//       }
//   });
// });
module.exports = router;
